﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class packetDal
    {
        /// <summary>
        /// 添加红包
        /// </summary>
        /// <param name="packet"></param>
        /// <returns></returns>
        public static int Addpacket(ms_packet packet)
        {
            string sql = "insert into ms_packet(p_merchantid,p_ruleid,p_money,p_kettype,p_ketcount,p_level,p_starttime,p_often,p_unit,p_endtime,p_way,p_addtime,p_rank,p_stype) values(@p_merchantid,@p_ruleid,@p_money,@p_kettype,@p_ketcount,@p_level,@p_starttime,@p_often,@p_unit,@p_endtime,@p_way,@p_addtime,@p_rank,@p_stype)";
            MySqlParameter[] values = new MySqlParameter[14];
            values[0] = new MySqlParameter("p_merchantid", packet.p_merchantid);
            values[1] = new MySqlParameter("p_ruleid", packet.p_ruleid);
            values[2] = new MySqlParameter("p_money", packet.p_money);
            values[3] = new MySqlParameter("p_kettype", packet.p_kettype);
            values[4] = new MySqlParameter("p_ketcount", packet.p_ketcount);
            values[5] = new MySqlParameter("p_level", packet.p_level);
            values[6] = new MySqlParameter("p_starttime", packet.p_starttime);
            values[7] = new MySqlParameter("p_often", packet.p_often);
            values[8] = new MySqlParameter("p_unit", packet.p_unit);
            values[9] = new MySqlParameter("p_endtime", packet.p_endtime);
            values[10] = new MySqlParameter("p_way", packet.p_way);
            values[11] = new MySqlParameter("p_addtime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[12] = new MySqlParameter("p_rank", packet.p_rank);
            values[13] = new MySqlParameter("p_stype", packet.p_stype);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 添加消费券
        /// </summary>
        /// <param name="packet"></param>
        /// <returns></returns>
        public static int Addcoupon(ms_packet packet)
        {
            string sql = "insert into ms_packet(p_merchantid,p_ruleid,p_money,p_kettype,p_ketcount,p_level,p_starttime,p_often,p_unit,p_endtime,p_way,p_addtime,p_rank,p_stype) values(@p_merchantid,@p_ruleid,@p_money,@p_kettype,@p_ketcount,@p_level,@p_starttime,@p_often,@p_unit,@p_endtime,@p_way,@p_addtime,@p_rank,@p_stype)";
            MySqlParameter[] values = new MySqlParameter[14];
            values[0] = new MySqlParameter("p_merchantid", packet.p_merchantid);
            values[1] = new MySqlParameter("p_ruleid", packet.p_ruleid);
            values[2] = new MySqlParameter("p_money", packet.p_money);
            values[3] = new MySqlParameter("p_kettype", packet.p_kettype);
            values[4] = new MySqlParameter("p_ketcount", packet.p_ketcount);
            values[5] = new MySqlParameter("p_level", packet.p_level);
            values[6] = new MySqlParameter("p_starttime", packet.p_starttime);
            values[7] = new MySqlParameter("p_often", packet.p_often);
            values[8] = new MySqlParameter("p_unit", packet.p_unit);
            values[9] = new MySqlParameter("p_endtime", packet.p_endtime);
            values[10] = new MySqlParameter("p_way", packet.p_way);
            values[11] = new MySqlParameter("p_addtime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[12] = new MySqlParameter("p_rank", packet.p_rank);
            values[13] = new MySqlParameter("p_stype", packet.p_stype);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查商户未结束红包
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static List<ms_packet> Querybymerchantid(string merchantid)
        {
            string sql = "select * from ms_packet where p_merchantid=@p_merchantid and p_isdel=1 and  TIMESTAMP(now())<TIMESTAMP(p_endtime)";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("p_merchantid",merchantid);
            values[1] = new MySqlParameter("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            List<ms_packet> packetlist = new List<ms_packet>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_packet Packet = new ms_packet();
                Packet.p_id = rd["p_id"].ToString();
                Packet.p_merchantid = rd["p_merchantid"].ToString();
                Packet.p_ruleid = rd["p_ruleid"].ToString();
                Packet.p_money = rd["p_money"].ToString();
                Packet.p_kettype = rd["p_kettype"].ToString();
                Packet.p_ketcount = rd["p_ketcount"].ToString();
                Packet.p_level = rd["p_level"].ToString();
                Packet.p_starttime = rd["p_starttime"].ToString();
                Packet.p_often = rd["p_often"].ToString();
                Packet.p_unit = rd["p_unit"].ToString();
                Packet.p_endtime = rd["p_endtime"].ToString();
                Packet.p_way = rd["p_way"].ToString();
                Packet.p_rank = rd["p_rank"].ToString();
                Packet.p_status = rd["p_status"].ToString();
                Packet.p_isdel = rd["p_isdel"].ToString();
                Packet.p_addtime = rd["p_addtime"].ToString();
                packetlist.Add(Packet);
            }
            rd.Close();
            return packetlist;
        }
        /// <summary>
        /// 通过类型未结束活动
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="p_stype"></param>
        /// <returns></returns>
        public static List<ms_packet> Querybymerchantid(string merchantid,string p_stype)
        {
            string sql = "select * from ms_packet where p_merchantid=@p_merchantid and p_stype=@p_stype and p_isdel=1 and  TIMESTAMP(now())<TIMESTAMP(p_endtime)";
            MySqlParameter[] values = new MySqlParameter[3];
            values[0] = new MySqlParameter("p_merchantid", merchantid);
            values[1] = new MySqlParameter("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[2] = new MySqlParameter("p_stype", p_stype);
            List<ms_packet> packetlist = new List<ms_packet>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_packet Packet = new ms_packet();
                Packet.p_id = rd["p_id"].ToString();
                Packet.p_merchantid = rd["p_merchantid"].ToString();
                Packet.p_ruleid = rd["p_ruleid"].ToString();
                Packet.p_money = rd["p_money"].ToString();
                Packet.p_kettype = rd["p_kettype"].ToString();
                Packet.p_ketcount = rd["p_ketcount"].ToString();
                Packet.p_level = rd["p_level"].ToString();
                Packet.p_starttime = rd["p_starttime"].ToString();
                Packet.p_often = rd["p_often"].ToString();
                Packet.p_unit = rd["p_unit"].ToString();
                Packet.p_endtime = rd["p_endtime"].ToString();
                Packet.p_way = rd["p_way"].ToString();
                Packet.p_rank = rd["p_rank"].ToString();
                Packet.p_status = rd["p_status"].ToString();
                Packet.p_isdel = rd["p_isdel"].ToString();
                Packet.p_addtime = rd["p_addtime"].ToString();
                packetlist.Add(Packet);
            }
            rd.Close();
            return packetlist;
        }
        /// <summary>
        /// 查商户已结束活动
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static List<ms_packet> Querybymerchantidend(string merchantid)
        {
            string sql = "select * from ms_packet where p_merchantid=@p_merchantid  and p_isdel=1 and  TIMESTAMP(now())>TIMESTAMP(p_endtime)";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("p_merchantid", merchantid);
            values[1] = new MySqlParameter("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            List<ms_packet> packetlist = new List<ms_packet>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_packet Packet = new ms_packet();
                Packet.p_id = rd["p_id"].ToString();
                Packet.p_merchantid = rd["p_merchantid"].ToString();
                Packet.p_ruleid = rd["p_ruleid"].ToString();
                Packet.p_money = rd["p_money"].ToString();
                Packet.p_kettype = rd["p_kettype"].ToString();
                Packet.p_ketcount = rd["p_ketcount"].ToString();
                Packet.p_level = rd["p_level"].ToString();
                Packet.p_starttime = rd["p_starttime"].ToString();
                Packet.p_often = rd["p_often"].ToString();
                Packet.p_unit = rd["p_unit"].ToString();
                Packet.p_endtime = rd["p_endtime"].ToString();
                Packet.p_way = rd["p_way"].ToString();
                Packet.p_rank = rd["p_rank"].ToString();
                Packet.p_status = rd["p_status"].ToString();
                Packet.p_isdel = rd["p_isdel"].ToString();
                Packet.p_addtime = rd["p_addtime"].ToString();
                packetlist.Add(Packet);
            }
            rd.Close();
            return packetlist;
        }
        /// <summary>
        /// 通过红包类型查已结束活动
        /// </summary>
        /// <param name="merchantid"></param>
        /// <param name="p_stype"></param>
        /// <returns></returns>
        public static List<ms_packet> Querybymerchantidend(string merchantid,string p_stype)
        {
            string sql = "select * from ms_packet where p_merchantid=@p_merchantid and p_stype=@p_stype  and p_isdel=1 and  TIMESTAMP(now())>TIMESTAMP(p_endtime)";
            MySqlParameter[] values = new MySqlParameter[3];
            values[0] = new MySqlParameter("p_merchantid", merchantid);
            values[1] = new MySqlParameter("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[2] = new MySqlParameter("p_stype", p_stype);
            List<ms_packet> packetlist = new List<ms_packet>();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
                ms_packet Packet = new ms_packet();
                Packet.p_id = rd["p_id"].ToString();
                Packet.p_merchantid = rd["p_merchantid"].ToString();
                Packet.p_ruleid = rd["p_ruleid"].ToString();
                Packet.p_money = rd["p_money"].ToString();
                Packet.p_kettype = rd["p_kettype"].ToString();
                Packet.p_ketcount = rd["p_ketcount"].ToString();
                Packet.p_level = rd["p_level"].ToString();
                Packet.p_starttime = rd["p_starttime"].ToString();
                Packet.p_often = rd["p_often"].ToString();
                Packet.p_unit = rd["p_unit"].ToString();
                Packet.p_endtime = rd["p_endtime"].ToString();
                Packet.p_way = rd["p_way"].ToString();
                Packet.p_rank = rd["p_rank"].ToString();
                Packet.p_status = rd["p_status"].ToString();
                Packet.p_isdel = rd["p_isdel"].ToString();
                Packet.p_addtime = rd["p_addtime"].ToString();
                packetlist.Add(Packet);
            }
            rd.Close();
            return packetlist;
        }
        /// <summary>
        /// 通过红包活动ID查详情
        /// </summary>
        /// <param name="p_id"></param>
        /// <returns></returns>
        public static ms_packet Querybyid(string p_id)
        {
            string sql = "select * from ms_packet where p_id=@p_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("p_id", p_id);
            ms_packet Packet = new ms_packet();
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            while (rd.Read())
            {
               
                Packet.p_id = rd["p_id"].ToString();
                Packet.p_merchantid = rd["p_merchantid"].ToString();
                Packet.p_ruleid = rd["p_ruleid"].ToString();
                Packet.p_money = rd["p_money"].ToString();
                Packet.p_kettype = rd["p_kettype"].ToString();
                Packet.p_ketcount = rd["p_ketcount"].ToString();
                Packet.p_level = rd["p_level"].ToString();
                Packet.p_starttime = rd["p_starttime"].ToString();
                Packet.p_often = rd["p_often"].ToString();
                Packet.p_unit = rd["p_unit"].ToString();
                Packet.p_endtime = rd["p_endtime"].ToString();
                Packet.p_way = rd["p_way"].ToString();
                Packet.p_rank = rd["p_rank"].ToString();
                Packet.p_status = rd["p_status"].ToString();
                Packet.p_isdel = rd["p_isdel"].ToString();
                Packet.p_addtime = rd["p_addtime"].ToString();
                
            }
            rd.Close();
            return Packet;
        }
    }
}
